package DesignPattern.prototype;

/**
 * @Author cj
 * @Date 2024/8/10 20:45
 */
public class shallowClone {


}

class Address {
    String city;

    public Address(String city) {
        this.city = city;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    public Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // 浅拷贝
    }

    @Override
    public String toString() {
        return name + " lives in " + address.city;
    }

    public static void main(String[] args) {
        try {
            Address address = new Address("New York");
            Person original = new Person("John", address);
            Person copy = (Person) original.clone();

            copy.name = "Doe";
            copy.address.city = "San Francisco";

            System.out.println(original); // 输出: John lives in San Francisco
            System.out.println(copy);     // 输出: Doe lives in San Francisco
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

